<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Articles extends Admin_Controller {
	
	function __construct()
	{
		parent::__construct(1, 2, 8);
	}
	
	function index()
	{
		$vData = array(
			'pages' => $this->admin->getArticles()
		);

		$vData['blogs'] = setKeys($this->admin->getBlogs(extractFieldFromDataArray($vData['pages'], 'tBlog')), 'id');
		$vData['users'] = $this->user->getUsernamesByIds(extractFieldFromDataArray($vData['pages'], 'tUserCreated'));
		
		$data = array(
			'content' => $this->load->view('admin/articles', $vData, true),
			'addJS' => array('/js/admin/article_list.js')
		);

		$this->getPage($data);
	}

	function deleted()
	{
		if (!$this->user->checkRight(2))
			redirect('/admin/articles');

		$vData = array(
			'pages' => $this->admin->getArticles(null, true, true),
			'deleted' => true,
		);

		$vData['blogs'] = setKeys($this->admin->getBlogs(extractFieldFromDataArray($vData['pages'], 'tBlog')), 'id');
		$vData['users'] = $this->user->getUsernamesByIds(extractFieldFromDataArray($vData['pages'], 'tUserCreated'));
		
		$data = array(
			'content' => $this->load->view('admin/articles', $vData, true)
		);
		
		$this->getPage($data);
	}
	
	function checkShortUrl()
	{
		if ($this->admin->existsUrl($_POST['url'], isset($_POST['id'])?$_POST['id']:0))
			die('1');
			else
			die('0');
	}
	
	function add($id = 0)
	{
		if (sizeof($_POST))
		{
			if (!($this->user->checkRight(2)))
			{
				if ($id = (isset($_POST['id']))?abs($_POST['id']):0)
				{
					$page = $this->admin->getArticle($id);
					if (empty($page) || $this->user->id != $page->tUserCreated) {
						redirect('/admin/articles');
					}
				}
			}

			$id = $this->admin->saveArticle($_POST);
			redirect('/admin/articles/add/'.$id);
		}
		
		$vData = array(
			'blocks' => $this->admin->getBlogs(),
			'page' => ($id) ? $this->admin->getArticle($id) : array(),
			'categories' => $this->admin->getCategories(),
			'articleCategories' => ($id) ? $this->main_model->getArticleCategories($id) : array(),
			'addStyles' => $this->admin->getAddStyles($id, 1),
			'addScripts' => $this->admin->getAddScripts($id, 1)
		);
		
		if (!($this->user->checkRight(2) || (empty($vData['page']) || $this->user->id == $vData['page']->tUserCreated)))
			redirect('/admin/articles');

		$data = array(
			'content' => $this->load->view('admin/articles_add', $vData, true),
			'addJS' => array(
				'/js/markitup/jquery.markitup.js',
				'/js/jquery/multiple-select/jquery.multiple.select.js',
				'/js/markitup/sets/default/set.js',
				'/js/tiny_mce/jquery.tinymce.js',
				'/js/tiny_mce/tiny_mce_settings.js',
				'/js/ajaxupload.js',
				'/js/translit.js',
				'/js/jcrop/js/jquery.Jcrop.min.js',
				'/js/admin/add_article.js'
				
			),
			'addCSS' => array(
				'/js/markitup/skins/markitup/style.css',
				'/js/jquery/multiple-select/multiple-select.css',
				'/js/markitup/sets/default/style.css',
				'/js/jcrop/css/jquery.Jcrop.min.css',
				'/css/admin/add_article.css',
				'/css/admin/add_page.css'
			)
		);
		
		$this->getPage($data);
	}

	function loadPhoto()
	{
		if (empty($_FILES))
			die('0');

		$mUploadDir = 'uploads/users/'.$this->user->id.'/';
		$uploadDir = rootDir().$mUploadDir;
		
		if (strpos($_FILES['file']['type'], 'image/') !== 0)
			die('2');

		if (!is_dir($uploadDir))
			mkdir($uploadDir);
		
		$uploadFile = now().'_'.preg_replace('/[^\w\.]/i', '', $_FILES['file']['name']);
		
		if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadDir.$uploadFile))
			echo base_url().$mUploadDir.$uploadFile;
		else
			echo '1';
	}

	function articleManage($id = 0, $manage = 0)
	{
		if (!$this->user->checkRight(2))
			die();

		if (!($id = (int)$id) || !in_array($manage = (int)$manage, array(0,1,2,3)))
			die();
		
		$where = array(
			'id' => $id
		);

		if ($manage != 3)
			$where['dDeleted'] = 0;

		if (!$this->db->where($where)->count_all_results('article'))
			die();
		
		switch ($manage) {
			case 0:
				$upd = array(
					'dAllowed' => 0,
					'tUserAllowed' => $this->user->id
				);
				break;
			case 1:
				$upd = array(
					'dAllowed' => now(),
					'tUserAllowed' => $this->user->id
				);
				break;
			case 2:
				$upd = array(
					'dDeleted' => now(),
					'tUserDeleted' => $this->user->id
				);
				break;
			case 3:
				$upd = array(
					'dAllowed' => 0,
					'dDeleted' => 0,
					'tUserDeleted' => $this->user->id
				);
				break;
		}

		echo (int)$this->db->where('id', $id)->update('article', $upd);
	}
}

?>